package com.hspedu.newData.dmsxl.树;

import com.hspedu.newData.dmsxl.ds.TreeNode;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LC938二叉搜索树的范围和 {

    public int rangeSumBST(TreeNode root, int low, int high) {

        if (root==null){
            return 0;
        }
        int left = 0;
        int right = 0;
        int val = 0;
        if (root.val < low){
            left = rangeSumBST(root.right,low,high);
        }else if (root.val > high){
            right = rangeSumBST(root.left,low,high);
        }else {
            val = root.val + rangeSumBST(root.right,low,high) + rangeSumBST(root.left,low,high);
        }

        return val + left + right;
    }
}
